/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = frstat_system_module_conn_add.c                    */
/*  NOTE      =                                                    */
/*  DATE      = 2015/01/30 by guojq                                */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "frhash2.h"
#include "frstat_system.h"
#include "apmtest_cunit.h"

extern int frstat_system_module_conn_add(int i_l7_proto, int i_syn, int i_established, int i_disconnect, int i_destroy);

static int g_i_flag_run =1;
static unsigned long g_ul_pcap_queue_used=0L;

static unsigned int frame_threads_cnt(void)
{
    return 0;
}

static int frtaskskb_counts(void)
{
    return 0;
}

//conn_data 连接信息
typedef struct __conn_data_status {
    unsigned long ul_syn_conns;
    unsigned long ul_established_conns;
    unsigned long ul_disconnect_conns;
    unsigned long ul_not_active_conns;
    unsigned long ul_destroy_conns;
} conn_data_status;

typedef struct __conn_data_avg {
    unsigned long ul_syn_conns[4];
    unsigned long ul_established_conns[4];
    unsigned long ul_disconnect_conns[4];
} conn_data_avg;

typedef struct __conn_data {
    conn_data_status st_conn_data_total;
    conn_data_avg    st_conn_data_avg;
} conn_data;

//session_data session信息
typedef struct __session_data_status {
    unsigned long ul_login;
    unsigned long ul_logout;
} session_data_status;

typedef struct __session_data_avg {
    unsigned long ul_login[4];
    unsigned long ul_logout[4];
} session_data_avg;

typedef struct __session_data {
    session_data_status st_session_data_total;
    session_data_avg st_session_data_avg;
} session_data;

//总模块信息
typedef struct __module_stat {
    int  i_l7_proto;
    char *p_module_name;
    
    conn_data       st_conn_data;
    session_data    st_session_data;
} module_stat;

//总体的 包速率、流量、连接数
typedef struct __conn_stat {
    unsigned long ul_packets;   //数据包总数
    unsigned long ul_bytes;     //总流量
    unsigned long ul_conns;     //总连接数
    unsigned long ul_drops;     //总丢包数
} conn_stat;

typedef struct __conn_stat_avg {
    unsigned long ul_packets[4];//数据包总数
    unsigned long ul_bytes[4];  //总流量
    unsigned long ul_conns[4];  //总连接数
    unsigned long ul_drops[4];  //总丢包数
} conn_stat_avg;

//总体运行信息
typedef struct __system_stat {
    struct timeval start_time;  //程序开始运行时间
    unsigned int ui_memory;     //内存
    int i_pthreads;             //线程数
    
    conn_stat st_conn_total;    //总连接信息
    conn_stat_avg st_conn_avg;  //平均总连接信息

    Frhash_header st_module_stat;      //模块信息hash
    
    int last_time;              //毫秒
} system_stat;

extern system_stat g_st_system;

static int __suite_init(void)
{
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}

void case1(void)
{
    int i_ret = 0;
    
    frstat_system_init(1);
    
    i_ret = frstat_system_module_conn_add(1433, 100, 100, 100, 100);

    //frstat_system_output(NULL);
    
    frstat_system_destroy();
    
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    return;
}

void case2(void)
{
    module_stat *p_module_node = NULL;
    Frlist_node* p_node = NULL;
    int i_ret = 0;
    
    frstat_system_init(1);
    
    p_module_node = (module_stat *)frmalloc(sizeof(module_stat));
    
    memset(p_module_node, 0x00, sizeof(module_stat));
    
    p_module_node->i_l7_proto = 1433;
    p_module_node->p_module_name = frstrdup(" ");
    
    p_module_node->st_conn_data.st_conn_data_total.ul_syn_conns = 100;
    p_module_node->st_conn_data.st_conn_data_total.ul_established_conns = 100;
    p_module_node->st_conn_data.st_conn_data_total.ul_disconnect_conns = 100;
    p_module_node->st_conn_data.st_conn_data_total.ul_not_active_conns = 0;
    p_module_node->st_conn_data.st_conn_data_total.ul_destroy_conns = 100;
    
    p_node = frlist_node_create(p_module_node);
    
    frhash2_addLong(&g_st_system.st_module_stat, 1433, p_node);

    i_ret = frstat_system_module_conn_add(1433, -10, -10, -10, -10);

    //frstat_system_output(NULL);
    
    frstat_system_destroy();
    
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    return;

}

void case3(void)
{
    module_stat *p_module_node = NULL;
    Frlist_node* p_node = NULL;
    int i_ret = 0;
    
    frstat_system_init(1);
    
    p_module_node = (module_stat *)frmalloc(sizeof(module_stat));
    
    memset(p_module_node, 0x00, sizeof(module_stat));
    
    p_module_node->i_l7_proto = 1425;
    p_module_node->p_module_name = frstrdup(" ");
    
    p_module_node->st_conn_data.st_conn_data_total.ul_syn_conns = 100;
    p_module_node->st_conn_data.st_conn_data_total.ul_established_conns = 100;
    p_module_node->st_conn_data.st_conn_data_total.ul_disconnect_conns = 100;
    p_module_node->st_conn_data.st_conn_data_total.ul_not_active_conns = 0;
    p_module_node->st_conn_data.st_conn_data_total.ul_destroy_conns = 100;
    
    p_node = frlist_node_create(p_module_node);
    
    frhash2_addLong(&g_st_system.st_module_stat, 1425, p_node);

    i_ret = frstat_system_module_conn_add(1433, 100, 100, 100, 100);

    //frstat_system_output(NULL);
    
    frstat_system_destroy();
    
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    return;

}

void case4(void)
{
    module_stat *p_module_node = NULL;
    Frlist_node* p_node = NULL;
    int i_ret = 0;
    
    frstat_system_init(1);
    
    p_module_node = (module_stat *)frmalloc(sizeof(module_stat));
    
    memset(p_module_node, 0x00, sizeof(module_stat));
    
    p_module_node->i_l7_proto = 1425;
    p_module_node->p_module_name = frstrdup(" ");
    
    p_module_node->st_conn_data.st_conn_data_total.ul_syn_conns = 100;
    p_module_node->st_conn_data.st_conn_data_total.ul_established_conns = 100;
    p_module_node->st_conn_data.st_conn_data_total.ul_disconnect_conns = 100;
    p_module_node->st_conn_data.st_conn_data_total.ul_not_active_conns = 0;
    p_module_node->st_conn_data.st_conn_data_total.ul_destroy_conns = 100;
    
    p_node = frlist_node_create(p_module_node);
    
    frhash2_addLong(&g_st_system.st_module_stat, 1425, p_node);

    i_ret = frstat_system_module_conn_add(1433, -10, -10, -10, -10);
    
    frstat_system_destroy();
    
    CU_ASSERT_EQUAL(i_ret,FR_FAIL);

    return;

}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("frstat_system_module_conn_add 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);
   	CU_add_test(pSuite, "case2", case2);
   	CU_add_test(pSuite, "case3", case3);
   	CU_add_test(pSuite, "case4", case4);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();
    return 0;
}